import { createSlice } from '@reduxjs/toolkit'
//以点击量为例
let hit = createSlice({
    name: 'hitcount',
    initialState: {
        hits: 1000
    },
    reducers: {
        //添加动作方法,默认都添加到了这个切片对象的actions属性中
        addHits: (state, { payload }) => {
            state.hits += payload;
        }
    }
});

export let { addHits } = hit.actions;

export let asyncaddHitCount = payload => {
    //返回的这个函数恰好是dispatch函数
    return dispatch => {
        setTimeout(() => {
            //同步方法
            dispatch(addHits(payload))
        }, 1000);
    }
}

export default hit;